package com.ymk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ymk.constant.RedisConstant;
import com.ymk.dao.SetmealDao;
import com.ymk.dao.TravelGroupDao;
import com.ymk.pojo.Setmeal;
import com.ymk.pojo.TravelGroup;
import com.ymk.service.SetMealService;
import com.ymk.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    SetmealDao setmealDao;

    @Autowired
    TravelGroupDao travelGroupDao;

    @Autowired
    JedisPool jedisPool;

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public void add(Setmeal setmeal, Integer[] travelGroupIds) {
        //新增套餐
        setmealDao.add(setmeal);
        //2.向套餐和跟团游的中间表中插入数据
        if(travelGroupIds != null && travelGroupIds.length > 0){
            //绑定套餐和跟团游的多对多关系
            setSetMealAndTravelGroup(setmeal.getId(),travelGroupIds);
        }
        //将图片名称保存到Redis
        savePic2Redis(setmeal.getImg());
    }

    //将图片名称保存到Redis
    private void savePic2Redis(String img) {
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
    }

    private void setSetMealAndTravelGroup(Integer id, Integer[] travelGroupIds) {
        for (Integer travelGroupId : travelGroupIds) {
            Map<String,Integer> map = new HashMap<>();
            map.put("travelgroup_id",travelGroupId);
            map.put("setmeal_id",id);
            setmealDao.setSetMealAndTravelGroup(map);
        }
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Integer> findTravelGroupItemBySetMealId(Integer id) {
        return setmealDao.findTravelGroupItemBySetMealId(id);
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] travelItemIds) {
        //1.修改套餐游的数据
        setmealDao.edit(setmeal);
        /**
         * 2.修改套餐游和跟团游行的中间表（先删除，在创建）
         * 主键的数据删除
         * 在新增页面选中的数据
         */
        setmealDao.deleteTravelGroupAndSetmealItemBySetmealId(setmeal.getId());
        //再新增页面选中的数据
        //3.向套餐和跟团游的中间表中插入数据
        if(travelItemIds != null && travelItemIds.length > 0){
            //绑定套餐和跟团游的多对多关系
            setSetMealAndTravelGroup(setmeal.getId(),travelItemIds);
        }
    }

    @Override
    public void delete(Integer id) {
        //在删除之前，先判断跟团游的id,在中间表中是否存在数据
        long count = travelGroupDao.findCountByTravelGroupItemId(id);
        //中间表如果有数据，不要往后面执行，直接抛出异常
        //如果非要删除也可以，delete from t_travelgroup_travelitem where travelitem_id = 1
        if(count > 0){
            throw new RuntimeException("不允许删除");
        }
        setmealDao.delete(id);
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }

}
